package com.woniu.cinema.service.impl;

import java.util.Date;
import java.util.List;

import com.woniu.cinema.domain.Hall;
import com.woniu.cinema.domain.Seat;
import com.woniu.cinema.mapper.HallMapper;
import com.woniu.cinema.mapper.SeatMapper;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import com.woniu.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.cinema.mapper.HallMovieMapper;
import com.woniu.cinema.domain.HallMovie;
import com.woniu.cinema.service.HallMovieService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 电影和影厅排片Service业务层处理
 * 
 * @author yy
 * @date 2021-04-16
 */
@Service
public class HallMovieServiceImpl implements HallMovieService
{
    @Autowired
    private HallMovieMapper hallMovieMapper;

    @Autowired
    private HallMapper hallMapper;


    @Autowired
    private SeatMapper seatMapper;


    /**
     * 查询电影和影厅排片
     * 
     * @param hallMovieId 电影和影厅排片ID
     * @return 电影和影厅排片
     */
    @Override
    public HallMovie selectHallMovieById(Long hallMovieId)
    {
        return hallMovieMapper.selectHallMovieById(hallMovieId);
    }

    /**
     * 查询电影和影厅排片列表
     * 
     * @param hallMovie 电影和影厅排片
     * @return 电影和影厅排片
     */
    @Override
    public List<HallMovie> selectHallMovieList(HallMovie hallMovie)
    {
        return hallMovieMapper.selectHallMovieList(hallMovie);
    }

    /**
     * 新增电影和影厅排片
     * 
     * @param hallMovie 电影和影厅排片
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHallMovie(HallMovie hallMovie)
    {
        Date startTime =DateUtils.addHours(hallMovie.getStartTime(),-3);
        Date endTime = DateUtils.addHours(hallMovie.getEndTime(),3);

        List<HallMovie> hallMovieList = hallMovieMapper.selectHallMovieByHallIdAndTime(hallMovie.getHallId(), startTime, endTime);
        for (HallMovie hm : hallMovieList) {

            if(hm.getStartTime().getTime()>=hallMovie.getStartTime().getTime()
                    && hm.getStartTime().getTime()<=hallMovie.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }
            if(hm.getEndTime().getTime()>=hallMovie.getStartTime().getTime()
                    && hm.getEndTime().getTime()<=hallMovie.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

            if(hallMovie.getStartTime().getTime()>=hm.getStartTime().getTime()
                    && hallMovie.getStartTime().getTime()<=hm.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

            if(hallMovie.getEndTime().getTime()>=hm.getStartTime().getTime()
                    && hallMovie.getEndTime().getTime()<=hm.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

        }

        Hall hall = hallMapper.selectHallById(hallMovie.getHallId());
        hallMovieMapper.insertHallMovie(hallMovie);

        Long seatY = hall.getHallY();
        Long seatX = hall.getHallX();

        for(long i=0;i<seatY;i++){
            for(long j=0;j<seatX;j++){
                Seat seat = new Seat();
                seat.setSeatX(j);
                seat.setSeatY(i);
                seat.setSeatStatus(1L);
                seat.setHallId(hallMovie.getHallId());
                seat.setHallMovieId(hallMovie.getHallMovieId());
                seat.setSeatName((i+1)+"排"+((j+1)+"座"));
                seatMapper.insertSeat(seat);
            }
        }

        return 1;
    }

    /**
     * 修改电影和影厅排片
     * 
     * @param hallMovie 电影和影厅排片
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHallMovie(HallMovie hallMovie)
    {

        //删除座位信息
        seatMapper.deleteSeatByHallMovieId(hallMovie.getHallMovieId());

        //删除排片信息
        hallMovieMapper.deleteHallMovieById(hallMovie.getHallMovieId());


        //检查排片时间是否冲突
        Date startTime =DateUtils.addHours(hallMovie.getStartTime(),-3);
        Date endTime = DateUtils.addHours(hallMovie.getEndTime(),3);

        List<HallMovie> hallMovieList = hallMovieMapper.selectHallMovieByHallIdAndTime(hallMovie.getHallId(), startTime, endTime);
        for (HallMovie hm : hallMovieList) {

            if(hm.getStartTime().getTime()>=hallMovie.getStartTime().getTime()
                    && hm.getStartTime().getTime()<=hallMovie.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }
            if(hm.getEndTime().getTime()>=hallMovie.getStartTime().getTime()
                    && hm.getEndTime().getTime()<=hallMovie.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

            if(hallMovie.getStartTime().getTime()>=hm.getStartTime().getTime()
                    && hallMovie.getStartTime().getTime()<=hm.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

            if(hallMovie.getEndTime().getTime()>=hm.getStartTime().getTime()
                    && hallMovie.getEndTime().getTime()<=hm.getEndTime().getTime()){
                throw new CustomException(ExceptionCode.PLAY_TIME_CONFILICTION);
            }

        }

        Hall hall = hallMapper.selectHallById(hallMovie.getHallId());
        //添加排片信息
        hallMovieMapper.insertHallMovie(hallMovie);

        Long seatY = hall.getHallY();
        Long seatX = hall.getHallX();

        //添加座位信息
        for(long i=0;i<seatY;i++){
            for(long j=0;j<seatX;j++){
                Seat seat = new Seat();
                seat.setSeatX(j);
                seat.setSeatY(i);
                seat.setSeatStatus(1L);
                seat.setHallId(hallMovie.getHallId());
                seat.setHallMovieId(hallMovie.getHallMovieId());
                seat.setSeatName((i+1)+"排"+((j+1)+"座"));
                seatMapper.insertSeat(seat);
            }
        }

        return 1;

    }

    /**
     * 批量删除电影和影厅排片
     * 
     * @param hallMovieIds 需要删除的电影和影厅排片ID
     * @return 结果
     */
    @Override
    public int deleteHallMovieByIds(Long[] hallMovieIds)
    {
        return hallMovieMapper.deleteHallMovieByIds(hallMovieIds);
    }

    /**
     * 删除电影和影厅排片信息
     * 
     * @param hallMovieId 电影和影厅排片ID
     * @return 结果
     */
    @Override
    public int deleteHallMovieById(Long hallMovieId)
    {
        return hallMovieMapper.deleteHallMovieById(hallMovieId);
    }
}
